const express = require('express');
const db = require('../utils/db');
const jwt = require('jsonwebtoken');

const router = express.Router();

// 登录
router.post('/login', (req, res) => {
    const { username, password, type } = req.body;
    let sql = '';
    if (type == '学生') {
        sql = `select * from t_student where s_user='${username}' and s_pass='${password}';`;
        db.query(sql, (err, data) => {
            if (err) {
                res.json({ error: 1, msg: err })
            } else {
                if (data.length > 0) {
                    //jwt.sign(签名的内容,密钥,有效时间)
                    let token = jwt.sign({ data: data[0] }, 'web308', {
                        expiresIn: 2000 * 60 * 30
                    });
                    let power = [
                        {
                            title: '首页公告',
                            href: '/home/noticeHome',
                            icon: 'el-icon-s-home',
                            com: "NoticeHome"
                        },
                        {
                            title: '学生管理',
                            href: '/home/student',
                            icon: 'el-icon-thumb',
                            com: "Student"
                        },
                        {
                            title: '富文本',
                            href: '/home/wangEditor',
                            icon: 'el-icon-thumb',
                            com: "wangEditor"
                        },
                        {
                            title: '图表eCharts',
                            href: '/home/eCharts',
                            icon: 'el-icon-thumb',
                            com: "eCharts"
                        },
                        {
                            title: '图表',
                            href: '/home/highCharts',
                            icon: 'el-icon-thumb',
                            com: "highCharts"
                        }
                    ]
                    res.json({ error: 0, data: data[0], msg: '登陆成功', token, power })
                } else {
                    res.json({ error: 1, msg: '账号密码错误' })
                }
            }
        })
    } else {
        sql = `select * from t_admin where a_user='${username}' and a_pass='${password}';`;
        db.query(sql, (err, data) => {
            if (err) {
                res.json({ error: 1, msg: err })
            } else {
                if (data.length > 0) {
                    //jwt.sign(签名的内容,密钥,有效时间)
                    let token = jwt.sign({ data: data[0] }, 'web308', {
                        expiresIn: 2000 * 60 * 30
                    });
                    let power;
                    if (data[0].a_status == "普通管理") {
                        power = [
                            {
                                title: '首页公告',
                                href: '/home/noticeHome',
                                icon: 'el-icon-s-home',
                                com: "NoticeHome"
                            },
                            {
                                title: '学生管理',
                                href: '/home/student',
                                icon: 'el-icon-thumb',
                                com: "Student"
                            },
                            {
                                title: '宿舍管理',
                                href: '/home/dorm',
                                icon: 'el-icon-bangzhu',
                                com: "Dorm"
                            },
                            {
                                title: '公告管理',
                                href: '/home/notice',
                                icon: 'el-icon-postcard',
                                com: "Notice"
                            }
                        ]
                    } else {
                        power = [
                            {
                                title: '首页公告',
                                href: '/home/noticeHome',
                                icon: 'el-icon-s-home',
                                com: "NoticeHome"
                            },
                            {
                                title: '学生管理',
                                href: '/home/student',
                                icon: 'el-icon-thumb',
                                com: "Student"
                            },
                            {
                                title: '宿舍管理',
                                href: '/home/dorm',
                                icon: 'el-icon-bangzhu',
                                com: "Dorm"
                            },
                            {
                                title: '公告管理',
                                href: '/home/notice',
                                icon: 'el-icon-postcard',
                                com: "Notice"
                            },
                            {
                                title: '管理员管理',
                                href: '/home/admin',
                                icon: 'el-icon-user-solid',
                                com: "Admin"
                            }
                        ]
                    }
                    res.json({ error: 0, data: data[0], msg: '登陆成功', token, power })
                } else {
                    res.json({ error: 1, msg: '账号密码错误' })
                }
            }
        })
    }

    // console.log(sql)
    //执行sql
    /* db.query(sql, (err, data) => {
        //err如果执行sql报错   data执行成功返回的结果
        if (err) {
            res.json({ error: 1, msg: err })
        } else {
            if (data.length > 0) {
                //jwt.sign(签名的内容,密钥,有效时间)
                let token = jwt.sign({ data: data[0] }, 'web308', {
                    expiresIn: '2min'
                    // 一般工作中半小时起 30min
                });
                res.json({ error: 0, data: data[0], msg: '登陆成功', token })
            } else {
                res.json({ error: 1, msg: '账号密码错误' })
            }
        }
    }) */
})
module.exports = router;
